/*
 *此javaCode由YMS-JDBC替换工具生成，原mapper位置com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper.DeviceLogMapper
 */
package com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper;

import com.yonyou.iuap.yms.api.IYmsJdbcApi;
import com.yonyou.iuap.yms.param.*;
import com.yonyou.iuap.yms.param.ResultSetProcessor;
import com.yonyou.iuap.yms.processor.*;
import com.yonyou.iuap.yms.replace.CollectionUtil;
import com.yonyou.iuap.yms.replace.YmsSqlUtil;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceLogEntity;

import java.util.*;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DeviceLogMapperImpl {
    
    @Autowired
    private IYmsJdbcApi ymsJdbcApi;

    public List<DeviceLogEntity> GetDeviceLog(String tenant_id, String name, String type, String opername, String devicecode, String devicename, String status, Integer startNumber, Integer pagesize) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" SELECT * ");
		sqlBuilder.append(" FROM isv_devicelog ");
		sqlBuilder.append(" WHERE ytenant_id = ? ");
		parameter.addParam(tenant_id);
		if (name != null && !"".equals(name)) {
			sqlBuilder.append(" and name like concat('%',?,'%') ");
			parameter.addParam(name);
		}
		if (type != null && !"".equals(type)) {
			sqlBuilder.append(" and type = ? ");
			parameter.addParam(type);
		}
		if (opername != null && !"".equals(opername)) {
			sqlBuilder.append(" and opername like concat('%',?,'%') ");
			parameter.addParam(opername);
		}
		if (devicecode != null && !"".equals(devicecode)) {
			sqlBuilder.append(" and devicecode like concat('%',?,'%') ");
			parameter.addParam(devicecode);
		}
		if (devicename != null && !"".equals(devicename)) {
			sqlBuilder.append(" and devicename like concat('%',?,'%') ");
			parameter.addParam(devicename);
		}
		if (status != null && !"".equals(status)) {
			sqlBuilder.append(" and status = ? ");
			parameter.addParam(status);
		}
		sqlBuilder.append(" And dr = 0 ");
		sqlBuilder.append(" limit ?,? ");
		parameter.addParam(startNumber);
		parameter.addParam(pagesize);

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new AutoBeanListProcessor(new DeviceLogEntity());
        return (List<DeviceLogEntity>) ymsJdbcApi.queryForList(sql, parameter, processor);
    }

    public int DeleteDeviceLog(String id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update isv_devicelog\n" + 
			         "set dr = 1          \n" + 
			         "where id = ?;       \n";
        parameter.addParam(id);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int DeleteDeviceLogByName(String name, String tenantId) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "update isv_devicelog           \n" + 
			         "set dr = 1                     \n" + 
			         "where name = ? and ytenant_id=?;\n";
        parameter.addParam(name);
		parameter.addParam(tenantId);

        
        return ymsJdbcApi.update(sql, parameter);
    }


    public DeviceLogEntity GetDeviceLogById(String tenant_id, String id) {
        SQLParameter parameter = new SQLParameter(true);
        
        String sql = "SELECT *           \n" + 
			         "FROM isv_devicelog \n" + 
			         "WHERE ytenant_id = ?\n" +
			         "AND id = ?         \n" + 
			         "And dr = 0         \n";
        parameter.addParam(tenant_id);
		parameter.addParam(id);

        ResultSetProcessor processor = new AutoBeanProcessor(new DeviceLogEntity());
        return (DeviceLogEntity) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }


    public int InsertDeviceLog(List<DeviceLogEntity> deviceList) {
		if (deviceList == null || deviceList.size()==0) return 0;
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" insert into isv_devicelog(id,`name`,`type`,opertime,opername,operuserid, devicecode, devicename, deviceinfo, status, ");
		sqlBuilder.append(" create_time, pubts, dr, ");
		sqlBuilder.append(" tenant_id, org_id, version,ytenant_id) ");
		sqlBuilder.append(" values ");
		if (CollectionUtil.isNotEmpty(deviceList)) {
			String separator0 = " , ";
			for (int index = 0; index < deviceList.size(); index++) {
				DeviceLogEntity item = deviceList.get(index);
				sqlBuilder.append(" (?,?,?,?,?,?,?,? ");
				parameter.addParam(item.getId());
				parameter.addParam(item.getName());
				parameter.addParam(item.getType());
				parameter.addParam(item.getOpertime());
				parameter.addParam(item.getOpername());
				parameter.addParam(item.getOperuserid());
				parameter.addParam(item.getDevicecode());
				parameter.addParam(item.getDevicename());
				sqlBuilder.append(" ,?,?,?,?,? ");
				parameter.addParam(item.getDeviceinfo());
				parameter.addParam(item.getStatus());
				parameter.addParam(item.getCreate_time());
				parameter.addParam(item.getPubts());
				parameter.addParam(item.getDr());
				sqlBuilder.append(" ,?,?,?,?) ");
				parameter.addParam(item.getTenant_id());
				parameter.addParam(item.getOrg_id());
				parameter.addParam(item.getVersion());
				parameter.addParam(item.getTenant_id());
				if (index < deviceList.size() - 1) {
					sqlBuilder.append(separator0);
				}
			}
		}

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int ModifyDeviceLog(DeviceLogEntity deviceLog) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" update isv_devicelog ");
		StringBuilder trimBuilder0 = new StringBuilder();
		trimBuilder0.append(" type = ? ");
		parameter.addParam(deviceLog.getType());
		if (deviceLog.getName() != null && !"".equals(deviceLog.getName())) {
			trimBuilder0.append(" name = ? ");
			parameter.addParam(deviceLog.getName());
		}
		if (deviceLog.getOpername() != null && !"".equals(deviceLog.getOpername())) {
			trimBuilder0.append(" opername = ? ");
			parameter.addParam(deviceLog.getOpername());
		}
		if (deviceLog.getOperuserid() != null && !"".equals(deviceLog.getOperuserid())) {
			trimBuilder0.append(" operuserid = ? ");
			parameter.addParam(deviceLog.getOperuserid());
		}
		if (deviceLog.getDevicename() != null && !"".equals(deviceLog.getDevicename())) {
			trimBuilder0.append(" devicename = ? ");
			parameter.addParam(deviceLog.getDevicename());
		}
		if (deviceLog.getDevicecode() != null && !"".equals(deviceLog.getDevicecode())) {
			trimBuilder0.append(" devicecode = ? ");
			parameter.addParam(deviceLog.getDevicecode());
		}
		if (deviceLog.getDeviceinfo() != null && !"".equals(deviceLog.getDeviceinfo())) {
			trimBuilder0.append(" deviceinfo = ? ");
			parameter.addParam(deviceLog.getDeviceinfo());
		}
		//status为基本类型int，不需要空判断
		if (deviceLog.getStatus() != null) {
			trimBuilder0.append(" status = ? ");
			parameter.addParam(deviceLog.getStatus());
		}
		if (deviceLog.getTenant_id() != null && !"".equals(deviceLog.getTenant_id())) {
			trimBuilder0.append(" ytenant_id = ? ");
			parameter.addParam(deviceLog.getTenant_id());
		}
		if (deviceLog.getOrg_id() != null && !"".equals(deviceLog.getOrg_id())) {
			trimBuilder0.append(" org_id = ? ");
			parameter.addParam(deviceLog.getOrg_id());
		}
		//version为基本类型int，不需要空判断
		if (deviceLog.getVersion() != null) {
			trimBuilder0.append(" version = ? ");
			parameter.addParam(deviceLog.getVersion());
		}
		sqlBuilder.append(YmsSqlUtil.trimSql(trimBuilder0, "SET", null, Collections.singletonList(","), Collections.singletonList(",")));
		sqlBuilder.append(" where id = ? and dr = 0 ");
		parameter.addParam(deviceLog.getId());

        
        String sql = sqlBuilder.toString();
        
        return ymsJdbcApi.update(sql, parameter);
    }


    public int GetDeviceLogCount(String tenant_id, String name, String type, String opername, String devicecode, String devicename, String status) {
        SQLParameter parameter = new SQLParameter(true);
        StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(" select count(*) from isv_devicelog ");
		sqlBuilder.append(" where ytenant_id = ? ");
		parameter.addParam(tenant_id);
		if (name != null && !"".equals(name)) {
			sqlBuilder.append(" and name like concat('%',?,'%') ");
			parameter.addParam(name);
		}
		if (type != null && !"".equals(type)) {
			sqlBuilder.append(" and type = ? ");
			parameter.addParam(type);
		}
		if (opername != null && !"".equals(opername)) {
			sqlBuilder.append(" and opername like concat('%',?,'%') ");
			parameter.addParam(opername);
		}
		if (devicecode != null && !"".equals(devicecode)) {
			sqlBuilder.append(" and devicecode like concat('%',?,'%') ");
			parameter.addParam(devicecode);
		}
		if (devicename != null && !"".equals(devicename)) {
			sqlBuilder.append(" and devicename like concat('%',?,'%') ");
			parameter.addParam(devicename);
		}
		if (status != null && !"".equals(status)) {
			sqlBuilder.append(" and status = ? ");
			parameter.addParam(status);
		}
		sqlBuilder.append(" and dr = 0 ");

        
        String sql = sqlBuilder.toString();
        ResultSetProcessor processor = new ColumnProcessor(Integer.class);
        return (int) ymsJdbcApi.queryForObject(sql, parameter, processor);
    }

}
